package com.dongdongshop.controller;

import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.user.TbAddress;
import com.dongdongshop.service.user.AddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户收货地址
 * @Author Eayon
 * @Date 2020/4/4 21:55
 */
@RestController
@RequestMapping("address")
public class AddressController {

    private static final Logger log = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private AddressService addressService;


    /**
     * 根据用户ID查询用户收货地址
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public DataResult<List<TbAddress>> findAddressByUserId(@PathVariable("userId")String userId){
        List<TbAddress> addresses = null;
        try {
            addresses = addressService.findAddressByUserId(userId);
        }catch (Exception e){
            log.error("根据用户ID查询用户收货地址异常",e);
            return new DataResult<List<TbAddress>>(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult<List<TbAddress>>(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),addresses);
    }

    /**
     * 根据主键ID查询用户收货地址
     * @param id
     * @return
     */
    @GetMapping("/byId/{id}")
    public DataResult<TbAddress> findAddressById(@PathVariable("id")Long id){
        TbAddress address = null;
        try {
            address = addressService.findAddressById(id);
        }catch (Exception e){
            log.error("根据主键ID查询收货地址异常",e);
            return new DataResult<TbAddress>(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult<TbAddress>(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage(),address);
    }

}
